namespace iReaper
{
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Runtime.CompilerServices;
    using System.Web.Configuration;

    public class DataManager
    {
        private static volatile SqlConnection conn;

        public static void Init()
        {
            InitDataBaseConnection();
        }

        public static DataSet GetDailyReportOfDownload()
        {
            SqlCommand selectCommand = conn.CreateCommand();
            selectCommand.CommandType = CommandType.Text;
            selectCommand.CommandText = "select convert(nvarchar,dayofYear,107) 日期 ,Amount Webcast下载数量 , ActiveUser 当日活动用户数, Average 日均每用户下载量 from dailyreportofdownload order by dayofYear";
            SqlDataAdapter adapter = new SqlDataAdapter(selectCommand);
            DataSet dataSet = new DataSet();
            adapter.Fill(dataSet);
            return dataSet;
        }

        public static int GetDistinctUserNumber()
        {
            SqlCommand command = conn.CreateCommand();
            command.CommandType = CommandType.Text;
            command.CommandText = "select count(distinct(userid)) from userbehavior";
            SqlDataReader reader = command.ExecuteReader();
            int num = 0;
            while (reader.Read())
            {
                num = reader.GetInt32(0);
                break;
            }
            reader.Close();
            return num;
        }

        public static DataSet GetDistinctUserNumberList()
        {
            SqlCommand selectCommand = conn.CreateCommand();
            selectCommand.CommandType = CommandType.Text;
            selectCommand.CommandText = "select Range 下载文件计数区间 ,Amount 用户数量 from RangStatics()";
            SqlDataAdapter adapter = new SqlDataAdapter(selectCommand);
            DataSet dataSet = new DataSet();
            adapter.Fill(dataSet);
            return dataSet;
        }


        [MethodImpl(MethodImplOptions.Synchronized)]
        private static void InitDataBaseConnection()
        {
            try
            {
                if (conn != null)
                {
                    conn.Close();
                }
                conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["ireaper"].ConnectionString);
                conn.Open();
            }
            catch
            {
                if (conn != null)
                {
                    conn.Close();
                }
                conn = null;
            }
        }

        public static void Log(LogRequest request)
        {
            SqlCommand command = conn.CreateCommand();
            command.CommandType = CommandType.Text;
            command.CommandText = "insert into downloadlog(UserID,CourseID,[Type],IP,[Version]) values (@UserID,@CourseID,@Type,@IP,@Version)";
            command.Parameters.Add("@CourseID", SqlDbType.Int).Value = request.CourseID;
            command.Parameters.Add("@UserID", SqlDbType.UniqueIdentifier).Value = request.UserID;
            command.Parameters.Add("@Type", SqlDbType.Int).Value = request.CourseType;
            command.Parameters.Add("@IP", SqlDbType.BigInt).Value = request.IP;
            command.Parameters.Add("@Version", SqlDbType.VarChar, 50).Value = request.UserAgent + "";

            command.ExecuteNonQuery();
        }

        public static void UpdateGeoInfo()
        {
            SqlCommand command = conn.CreateCommand();
            command.CommandType = CommandType.Text;
            command.CommandText = "select IP, City, Region, UserID from [user] where City is null";

            
            SqlDataAdapter sda = new SqlDataAdapter(command);
            SqlCommandBuilder scb = new SqlCommandBuilder(sda);
            DataSet ds = new DataSet();
            sda.Fill(ds);

            GeoInfoManager.UpdateTableSet(ds);

            sda.Update(ds);
        }
    }
}
